import 'package:flutter/material.dart';
import 'package:flutter_magic_planet/common/http/api.dart';
import 'package:flutter_magic_planet/common/http/api_video_bljiex.dart';
import 'package:flutter_magic_planet/common/http/api_video_pingcc.dart';
import 'package:flutter_magic_planet/component/toast.dart';
import 'package:flutter_magic_planet/model/video.dart';
import 'package:flutter_magic_planet/model/video_api_source.dart';
import 'package:get/get.dart';

class VideoListLogic extends GetxController {
  String? option;
  String? query;
  final videoApiSourceList = <VideoApiSource>[].obs;

  @override
  void onInit() {
    fetchApiSource();
    super.onInit();
  }

  init({option, required query, required TickerProvider vsync}) async {
    this.option = option;
    this.query = query;
    // await fetchApiSource();
    // print('videoApiSourceList.length ${videoApiSourceList.length}');

    // fetchVideos1();
    // fetchVideos2();
  }

  Future<List<VideoApiSource>> fetchApiSource() async {
    Toast.loading();
    List<VideoApiSource> _list = await API.getVideoApiSource();
    List<VideoApiSource> result = [];
    // 组装展示的数据
    for (var x in _list) {
      if (!(x.enabled ?? true)) {
        continue;
      }
      List<Video> _videos = [];
      switch (x.id) {
        case APIVideoPingcc.ID:
          _videos =
              await APIVideoPingcc.getVideos(key: query!, option: option!);
          break;
        case APIVideoBljiex.ID:
          _videos = await APIVideoBljiex.getVideos(key: query!);
          break;
      }
      x.videos = _videos;
      result.add(x);
    }
    videoApiSourceList.value = result;
    Toast.dismiss();
    return result;
  }
}
